﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
using System.Diagnostics;

namespace DBServer
{

    public class DBServer
    {
        private static DataReceivedEventHandler handler = new DataReceivedEventHandler(p_OutputDataReceived);
        private static StreamWriter sw;
        public DBServer()
        {
            
        }

        public static void StartListening()
        {
            Process p = new Process();
            p.StartInfo.FileName = @"..\..\..\DBShell\bin\Debug\DBShell.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = false;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            byte[] bytes = new Byte[1024];
            IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
            int port = 10000;
            TcpListener tcpListener = new TcpListener(ipAddress, port);
            Console.WriteLine("begin to listen " + ipAddress.ToString());
            p.OutputDataReceived += handler;
            p.BeginOutputReadLine();
            try
            {
                while (true)
                {
                    tcpListener.Start();
                    Socket socketForClient = tcpListener.AcceptSocket();
                    if (socketForClient.Connected)
                    {
                        if(sw!=null)
                        {
                            sw.Close();
                        }
                        Console.WriteLine("New Client connected");
                        NetworkStream networkStream = new NetworkStream(socketForClient);
                        StreamWriter streamWriter = new StreamWriter(networkStream);
                        StreamReader streamReader = new StreamReader(networkStream);
                        sw = streamWriter;
                        while (true)
                        {
                            string cmd = streamReader.ReadLine();
                            p.StandardInput.WriteLine(cmd);
                            p.StandardInput.Flush();
                            streamWriter.Flush();
                            if(cmd == "exit")
                            {
                                return;
                            }
                        }

                    }
                    tcpListener.Stop();
                }

            }
            catch (System.Exception e)
            {
                p.OutputDataReceived -= handler;
                p.StandardInput.WriteLine("exit");
                p.StandardInput.Flush();
                p.WaitForExit();
                sw.Close();
                tcpListener.Stop();
                StartListening();
            }

        }
        static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            sw.WriteLine(e.Data);
            Console.WriteLine(e.Data);
        }

    }
}
